package com.zkzx.truck.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zkzx.common.global.Global;
import com.zkzx.common.truck.constants.TruckStatusEnum;
import com.zkzx.common.truck.model.TruckListVO;
import com.zkzx.common.truck.param.TruckFilterParam;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.truck.client.CompanyClient;
import com.zkzx.truck.service.TruckService;
import com.zkzx.util.json.JSONResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/24/11:47
 * @Description:
 */
@Slf4j
@Component
public class TruckJob {

    @Autowired
    private TruckService truckService;
    @Autowired
    private CompanyClient companyClient;

    /**
     * 1、车辆状态查询任务
     */
    @XxlJob("truckStatusJobHandler")
    public void truckJobHandler(){

        XxlJobHelper.log("执行车辆审核状态查询更新");
        List<String> statusList = new ArrayList<>();
        //已有数据共享中，审核中
        statusList.add(TruckStatusEnum.APPROVE_ING.name());

        TruckFilterParam truckFilterParam = new TruckFilterParam();
        truckFilterParam.setStatusList(statusList);
        // 分页处理 每次查询100条
        RequestFilterParams params = new RequestFilterParams();
        params.setPage(Global.ONE);
        params.setPerPage(100);
        params.setOrder("");
        int totalNum = truckService.find(truckFilterParam,params).getTotalNum();
        log.info("执行车辆审核状态查询更新:{}", totalNum);

        // 当查询到没有数据
        if(totalNum <= Global.ZERO){
            return;
        }


        int num = (int) Math.ceil((float)totalNum/100);
        for (int i = 1; i <= num; i++) {
            params.setPage(i);
            PageResult<TruckListVO> result = truckService.find(truckFilterParam,params);
            result.getResult().forEach(vo -> truckService.updateToWh(vo));
        }
    }
}
